package com.osstem;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.osstem.model.Staff;
;

public class ChangeSystemActivity extends Activity{
    
	private DbManager dbManager;	//dbManager 객체
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.insert_menu);
        
        dbInit();				//db 설정 초기
        createLayout();			//화면을 초기화한다.        
    }
    
    /** 
     * Method   : dbInit
     * 최초작성일  : 2011. 11. 14. 
     * 작성자 : jw
     * 변경이력 :  
     * Method 설명 : db 초기화 method 
     */
    public void dbInit(){
    	dbManager = new DbManager(this);
    }
    
	/** 
     * Method   : createLayout
     * 최초작성일  : 2011. 11. 14. 
     * 작성자 : jw
     * 변경이력 :  
     * Method 설명 : 사원 정보를 조회한후 layout을 만들어준다.
     */
    public void createLayout(){
    	ArrayList<Staff> staffList = (ArrayList<Staff>)dbManager.selectStaff();		//직원 리스트 조회
    	LinearLayout subLinearLayout = (LinearLayout)findViewById(R.id.subLinearLayout);
    	
    	
    	for(Staff staff: staffList){
    		
    		//직원 이름
    		TextView name = new TextView(this);
    		name.setTextSize(20);
    		name.setTextColor(Color.BLACK);
    		name.setGravity(Gravity.CENTER);
    		name.setText("(" + staff.getName() +")");
    		
    		//환급금액
    		TextView amount = new TextView(this);
    		amount.setTextColor(Color.BLACK);
    		amount.setWidth(160);
    		amount.setTextSize(20);
    		amount.setGravity(Gravity.RIGHT);
    		amount.setText( "("  + staff.getAmount() +  " ) 원");
    		
    		//금액 입력버튼
    		Button button = new Button(this);
    		button.setText(staff.getSabun());
    		button.setOnClickListener(new OnClickListener(){
	    		public void onClick(View v) {
					Button button = (Button)v;
					Intent intent = new Intent( ChangeSystemActivity.this, InsertAmount.class);
					intent.putExtra("sabun", button.getText()); 
					startActivity(intent);
					finish();	
	    		}
    		});
    		
    		LinearLayout newlLinearLayout = new LinearLayout(this);
    		
    		newlLinearLayout.addView(name);
    		newlLinearLayout.addView(amount);
    		newlLinearLayout.addView(button);
    		
    		subLinearLayout.addView(newlLinearLayout);
    	}//end for
    	
    	//초기화 버튼
    	Button button = (Button)findViewById(R.id.init);
    	button.setOnClickListener(new OnClickListener(){
    		public void onClick(View v) {
    			dbManager.clearToday();
				Intent intent = new Intent( ChangeSystemActivity.this, ChangeSystemActivity.class); 
				startActivity(intent);
    		}
		});
    }
    
    
    @Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
    	Intent intent = new Intent(ChangeSystemActivity.this, Main.class);
		startActivity(intent);
		finish();
		return true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(dbManager != null){
			dbManager.closeReadalbeDb();
			dbManager.closeWritableDb();
		}
	}
    
}